// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// A [author] rhyme_lph
// s [email]  rhymelph@gmail.com
// T [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/entities/grant_entity.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/input/search_input_field.dart';
import 'package:finance_app/src/widgets/select/normal_radio.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/dashboard/cash_in/select_grant_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
export 'package:finance_app/src/entities/grant_entity.dart';

@RouterMeta(paramName: "selectGrantPage")
class SelectGrantPage extends StatefulWidget {
  final GrantEntity select;

  const SelectGrantPage({Key key, this.select}) : super(key: key);

  @override
  _SelectGrantPageState createState() => _SelectGrantPageState();
}

class _SelectGrantPageState extends State<SelectGrantPage>
    with PagePresenter<SelectGrantPage, SelectGrantProvider> {
  @override
  PreferredSizeWidget get appBar => getDefaultAppBar(
        title: S.current.grant,
      );

  @override
  Widget get buildWidget => ColoredBox(
        color: Colors.white,
        child: Column(
          children: [
            AppSpaces.h20,
            SearchInputField(
              controller: provider.searchController,
              focusNode: provider.searchFocusNode,
            ),
            AppSpaces.h20,
            Expanded(
                child: listBuilder<Map<String, List<GrantEntity>>>(
              selector: (_, __) => Map.from(__.groupMap),
              builder: (BuildContext context,
                      Map<String, List<GrantEntity>> value, Widget child) =>
                  ListView.builder(
                itemBuilder: (BuildContext context, int index) =>
                    _buildItems(value.entries.first.value[index]),
                itemCount: value.entries.first.value.length,
              ),
            ))
          ],
        ),
      );

  Widget _buildItems(GrantEntity value) {
    return PressedOpacityButton(
      onPressed: () => provider.onSelectGrant(value),
      padding: EdgeInsets.symmetric(
        vertical: getDp(10),
        horizontal: getDp(34),
      ),
      child: listBuilder<bool>(
        selector: (_, __) => __.currentSelect?.id == value.id,
        builder: (BuildContext context, bool isSelect, Widget child) =>
            NormalRadio(
          title: value.name,
          isFix: true,
          isSelect: isSelect,
        ),
      ),
    );
  }
}
